package com.zenchn.schedule.task;


import com.alibaba.fastjson.JSONObject;
import com.zenchn.entity.SysConfig;
import com.zenchn.model.TSysConfig;
//import com.zenchn.redis.service.IpBlackListRedisService;
import com.zenchn.service.ConfigService;
import com.zenchn.utils.ObjectUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 *  系统参数实例轮询加载
 *
 * @author panbingqi
 * @date 2021-06-01
 **/
@Component
public class SysConfigInit  implements ApplicationRunner {

	@Resource
	private SysConfig config;

	@Resource
	private ConfigService configService;

//	@Resource
//	private IpBlackListRedisService ipBlackListRedisService;

	/***
	 *  文件存储方式
	 */
	@Value("${custom.file.storage}")
	private String storage;

	/***
	 * 磁盘正式文件访问url
	 */
	@Value("${custom.file.formal.url}")
	private String formalUrl;
	/***
	 * 磁盘临时文件访问url
	 */
	@Value("${custom.file.temp.url}")
	private String tmpUrl;

	/***
	 * 当前服务器 与oss 之间网络 是否可内网通信
	 */
	@Value("${custom.file.alioss.network}")
	private String network;

	@Override
	public void run(ApplicationArguments args) throws Exception {
		main();
	}

	@Scheduled(initialDelay=60000, fixedDelay=60000)
	public void main() throws Exception {

		/**初始化系统配置参数*/

		List<TSysConfig> configList = configService.selectByExample(null);
		for (TSysConfig config : configList) {
			switch (config.getConfigCode()) {
				case "defaultPassword":
					this.config.setDefaultPassword(ObjectUtils.stringToObject(config.getValueStyle(),config.getConfigValue()));
					if (this.config.getDefaultPassword()==null || "".equals(this.config.getDefaultPassword().trim())){
						this.config.setDefaultPassword("123456");
					}
					break;
				case "smsUrl":
					this.config.setSmsUrl(ObjectUtils.stringToObject(config.getValueStyle(),config.getConfigValue()));
					break;
				case "smsAccessKeyId":
					this.config.setSmsAccessKeyId(ObjectUtils.stringToObject(config.getValueStyle(),config.getConfigValue()));
					break;
				case "smsAccessSecret":
					this.config.setSmsAccessSecret(ObjectUtils.stringToObject(config.getValueStyle(),config.getConfigValue()));
					break;
				case "xingePushInspectAccessId":
					this.config.setXingePushInspectAccessId(ObjectUtils.stringToObject(config.getValueStyle(),config.getConfigValue()));
					break;
				case "xingePushInspectAccessKey":
					this.config.setXingePushInspectAccessKey(ObjectUtils.stringToObject(config.getValueStyle(),config.getConfigValue()));
					break;
				case "xingePushInspectSecretKey":
					this.config.setXingePushInspectSecretKey(ObjectUtils.stringToObject(config.getValueStyle(),config.getConfigValue()));
					break;
				case "alioss_formal_file_url":
					this.config.setAliossFormalFileUrl(ObjectUtils.stringToObject(config.getValueStyle(),config.getConfigValue()));
					break;
				case "alioss_temp_file_url":
					this.config.setAliossTempFileUrl(ObjectUtils.stringToObject(config.getValueStyle(),config.getConfigValue()));
					break;
				case "alioss_formal_bucketName":
					this.config.setAliossFormalBucketName(ObjectUtils.stringToObject(config.getValueStyle(),config.getConfigValue()));
					break;
				case "alioss_temp_bucketName":
					this.config.setAliossTempBucketName(ObjectUtils.stringToObject(config.getValueStyle(),config.getConfigValue()));
					break;
				case "alioss_endPoint_extranet":
					this.config.setAliossEndPointExtranet(ObjectUtils.stringToObject(config.getValueStyle(),config.getConfigValue()));
					break;
				case "alioss_endPoint_intranet":
					this.config.setAliossEndPointIntranet(ObjectUtils.stringToObject(config.getValueStyle(),config.getConfigValue()));
					break;
				case "alioss_accessKeyId":
					this.config.setAliossAccessKeyId(ObjectUtils.stringToObject(config.getValueStyle(),config.getConfigValue()));
					break;
				case "alioss_accessKeySecret":
					this.config.setAliossAccessKeySecret(ObjectUtils.stringToObject(config.getValueStyle(),config.getConfigValue()));
					break;
				case "alioss_sts_regionId":
					this.config.setAliossStsRegionId(ObjectUtils.stringToObject(config.getValueStyle(),config.getConfigValue()));
					break;
				case "alioss_sts_endpoint":
					this.config.setAliossStsEndpoint(ObjectUtils.stringToObject(config.getValueStyle(),config.getConfigValue()));
					break;
				case "alioss_sts_roleArn":
					this.config.setAliossStsRoleArn(ObjectUtils.stringToObject(config.getValueStyle(),config.getConfigValue()));
					break;
				case "officeOnlinePreviewUrl":
					this.config.setOfficeOnlinePreviewUrl(ObjectUtils.stringToObject(config.getValueStyle(),config.getConfigValue()));
					break;
//				case "ip_blacklist":
//					List<String> ipBlackList = JSONObject.parseArray(ObjectUtils.stringToObject(config.getValueStyle(),config.getConfigValue()), String.class);
//					this.handleIpBlackList(ipBlackList);
//					break;
				case "terminalUrl":
					this.config.setTerminalUrl(ObjectUtils.stringToObject(config.getValueStyle(),config.getConfigValue()));
					break;
				case "terminalAccessKeyId":
					this.config.setTerminalAccessKeyId(ObjectUtils.stringToObject(config.getValueStyle(),config.getConfigValue()));
					break;
				case "terminalAccessSecret":
					this.config.setTerminalAccessSecret(ObjectUtils.stringToObject(config.getValueStyle(),config.getConfigValue()));
					break;
				default:
					break;
			}
		}

		config.setDiskFormalFileUrl(formalUrl);
		config.setDiskTempFileUrl(tmpUrl);

		//文件存储方式判断
		String disk="disk";
		if (storage.equals(disk)){
			config.setCurrentUseFormalFileUrl(config.getDiskFormalFileUrl());
		}else{
			config.setCurrentUseFormalFileUrl(config.getAliossFormalFileUrl());
		}

		/**
		 * 判断当前服务器 与oss 之间网络 是否可内网通信，true 使用内网链接，false 则使用外网链接
		 */
		//内网配置
		String intranet="intranet";
		if(intranet.equals(network)){
			config.setCurrSystemAliossEndPoint(config.getAliossEndPointIntranet());
		}else{
			config.setCurrSystemAliossEndPoint(config.getAliossEndPointExtranet());
		}
	}


//	private void handleIpBlackList(List<String> ipBlackList){
//		for (String ip:ipBlackList){
//			if (!ipBlackListRedisService.hasExist(ip)){
//				ipBlackListRedisService.add(ip);
//			}
//		}
//	}

}
